using System;
using System.Collections.Generic;
using System.Text;
using FE;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Game = FE.Game;
namespace WinXnaFE
{
    public class XnaGameAdapter : GameAdapter
    {
        public Dictionary<Type, Texture2D> SpriteMap = new Dictionary<Type, Texture2D>();
        private Game1 game1;
        private Game game;

        public Game1 Game1
        {
            get { return game1; }
        }
        public Game Game
        {
            get { return game; }
        }
        public XnaGameAdapter()
        {
            game1 = new Game1(this);
        }
        public void Start()
        {
            game = new Game(this);
        }
        public override void ActionChoice()
        {
        }
        public override void AddAction(Actions action)
        {
        }
        public override void ClearActions()
        {
        }
        public override void PerformMapInitialize(Map map)
        {
            PlayerControllerMap[map.PlayerOrder[0]] = PlayerIndex.One;
            PlayerControllerMap[map.PlayerOrder[1]] = PlayerIndex.One;
        }
        public override void PerformBattle(List<BattleResult> battleResults)
        {
        }
        public override void PerformMove(int x0, int y0, int x1, int y1)
        {
        }
        public override void PerformSelect(UnitStatus unit)
        {
            CurrentPlayerController = PlayerControllerMap[CurrentPlayer];
            StartTurnTask stt = new StartTurnTask(game1.TestFont, "Unit Selected", 2000);
            game1.currentTask = stt;
        }
        public PlayerIndex CurrentPlayerController = PlayerIndex.One;
        Dictionary<Player, PlayerIndex> PlayerControllerMap = new Dictionary<Player, PlayerIndex>();
        public override void PerformTurn(Player p)
        {
            CurrentPlayerController = PlayerControllerMap[p];
            StartTurnTask stt = new StartTurnTask(game1.TestFont,"Player Start Turn", 2000);
            game1.currentTask = stt;
        }
        public override void UpdateMap()
        {
        }
        public class StartTurnTask : GameTask
        {
            Timer t;
            SpriteFont font;
            string message;
            public StartTurnTask(SpriteFont font, string message, double delay)
            {
                this.message = message;
                this.font = font;
                this.t = new Timer(delay);
            }
            public override void Render(GraphicsDevice device, SpriteBatch batch, double elapsedTime)
            {
                batch.DrawString(font, message, new Vector2(100, 100), Color.Black);
                t.Update(elapsedTime);
                if (t.Ready) Finished = true;
            }
            public override void Input(GamePadState gamePadState, double elapsedTime)
            {
                if (gamePadState.Buttons.A == ButtonState.Pressed) Finished = true;
            }
        }
        public class GameTask
        {
            public virtual void Update(double elapsedTime)
            {

            }
            public virtual void Render(GraphicsDevice device, SpriteBatch batch, double elapsedTime)
            {

            }
            public virtual void Input(GamePadState gamePadState, double elapsedTime)
            {

            }
            private bool finished = false;
            public bool Finished 
            { 
                get { return finished; }
                set { finished = value; }            
            }
        }
    }
}
